/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.reserveView;
/**
 * 说明：
 * 其他方式出库单
 * @Author：黄盛强
 * @Date：2012-03-20
 */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TOutBill;
import com.ruipengkj.bean.TStore;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JComboBoxPOJO;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.JPanelGridLayoutComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.RPJButtonRoundBlue;
import com.ruipengkj.component.RPJButtonRoundGray;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.OtherReserveModel;
import com.ruipengkj.service.TOutBillService;
import com.ruipengkj.service.TStockInOutService;
import com.ruipengkj.service.TStoreService;
import com.ruipengkj.view.base.BasePagePanel;

public class OtherReservePanel extends BasePagePanel<TOutBill> implements  ActionListener, Runnable{

	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent orderID; //报损编号【输入采购单编号】
	private DataPackageComponent startTime;//开始时间
	private DataPackageComponent endTime;//结束时间
	
	private JButton queryButton;//查询
	private JButton clearButton;//清空
	private JButton showButton; //查看详细
	private JButton addButton; //添加进货单
	private JLabelJComboBoxComponent storeId;//仓库编号
	private JTable table;
	private JScrollPane tableJsp;
	private OtherReserveModel OtherReserveModel;
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TOutBill> pageView;//翻页主键
	private TStoreService tStoreService;//仓库编号
	private TOutBillService tOutBillService;//物品领用
	private TStockInOutService tStockInOutService;//出入库单
	
	public OtherReservePanel() {
		tOutBillService = BuildSpringApplication.getInstance().getTOutBillServiceImpl();
		tStoreService=BuildSpringApplication.getInstance().getTStoreServiceImpl();
		tStockInOutService = BuildSpringApplication.getInstance().getTStockInOutServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
	}
	private void init() {
		pageView = new PageView<TOutBill>();
		OtherReserveModel = new OtherReserveModel();
		table=uiFactory.createRPJTable(OtherReserveModel, null, this, 30, JLabel.CENTER);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		this.add(initTop(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(initTail(), BorderLayout.SOUTH);
	}
	private JPanel initTop() {
		QueryPanel queryPanel=new QueryPanel("其他方式出入库单",ImageFactory.loadImage(ImageFactory.FUN_5_ICON_4));
		JPanel top_panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		orderID = new JLabelJTextFieldComponent("单编号：", 20);
		startTime = new DataPackageComponent("开始时间", 188);
		endTime = new DataPackageComponent("结束时间", 188);
		queryButton = new RPJButtonBlue("查询");
		clearButton= new RPJButtonGray("清空");
		storeId = new JLabelJComboBoxComponent("仓库编号", new String[]{},188);
		storeId.addElement(new JComboBoxPOJO("请选择……"));
		top_panel.add(storeId);
		top_panel.add(orderID);
		top_panel.add(startTime);
		top_panel.add(endTime);
		top_panel.add(queryButton);
		top_panel.add(clearButton);
		queryPanel.addPanel(top_panel);
//		queryPanel.addBoxPanel(top_panel);
		return queryPanel;
	}
	
	private JPanel initTail() {
		addButton = new RPJButtonRoundBlue("添加(Ctrl+A)");
		showButton= new RPJButtonRoundGray("查看详细");
		
		JPanel jp2 = new JPanelFlowLayoutComponent(new Component[]{new JLabel("   "),addButton,showButton}, FlowLayout.LEFT);
		JPanel tail_panel = new JPanelGridLayoutComponent(new Component[]{jp2,pagingComponent}, 1, 2);
		return tail_panel;
	}
	

	private void initListener() {//添加监听器
		clearButton.addActionListener(this);
		queryButton.addActionListener(this);
		addButton.addActionListener(this);
		showButton.addActionListener(this);
		addButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == queryButton) {
			//查询
			this.termQuery(1);
		} else if(e.getSource() == addButton) {
			//添加
			OtherReserveAddDialog OtherReserveAddDialog = new OtherReserveAddDialog(null, "添加其他方式出库单", true);;
			OtherReserveAddDialog.showDialog(null);
			if(OtherReserveAddDialog.isSuccess){
				this.termQuery(1);
			}
		} else if(e.getSource()==showButton){
			//查看详细
			if(table.getSelectedRow()!=-1){
				String comsumeID = (String)table.getValueAt(table.getSelectedRow(), 0);
				OtherReserveAddDialog OtherReserveAddDialog = new OtherReserveAddDialog(null, "其他方式出库单详情", true);
				OtherReserveAddDialog.showDialog(comsumeID);
				if(OtherReserveAddDialog.isSuccess){
					this.termQuery(1);
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==clearButton){
			//清空
			this.clear();
		}
	}
	
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == table && e.getClickCount() == 2) {
			showButton.doClick();
		}
	}
	
	@Override
	public void run() {
		this.termQuery(1);
		SwingUtilities.invokeLater(queryStore);
	}
	
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		final TOutBill TOutBill=new TOutBill();
		TOutBill.setBillID(orderID.getText().getText().trim());
		TOutBill.setReasonType(ApplicationContext.OUTBILL_REASONTYPE3);
		TOutBill.setHappenTime(startTime.getDate());
		TOutBill.setEndTime(endTime.getDate());
		if(storeId.getBox().getSelectedIndex()!=0){
			JComboBoxPOJO storeIdPo=(JComboBoxPOJO)storeId.getBox().getSelectedItem();//仓库编号
			System.out.println(storeIdPo.getName());
			TOutBill.setStoreid(storeIdPo.getName());
		}
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TOutBill> pageModel = tOutBillService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),TOutBill);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						OtherReserveModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								TabelUtil.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						OtherReserveModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	

	//查询仓库编号
			private Runnable queryStore=new Runnable() {
				@Override
				public void run() {
					System.out.println("bbbb");
					try {
						List<TStore> list=tStoreService.findService(null);
						storeId.getModel().removeAllElements();
						storeId.addElement(new JComboBoxPOJO("请选择……"));
						if(list!=null&&list.size()!=0){
							int length = list.size();
							for(int i = 0; i < length; ++i) {
								TStore tStore=list.get(i);
								storeId.addElement(new JComboBoxPOJO(tStore.getStoreID(),tStore.getStoreID(),tStore));
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			};
	
	@Override
	protected PageView<TOutBill> getPageView() {
		return pageView;
	}
	@Override
	public void clear() {
		OtherReserveModel.clear();
		orderID.getText().setText("");
		startTime.setDate(null);
		endTime.setDate(null);
		storeId.getBox().setSelectedIndex(0);
	}
}